import time
import subprocess
from xiaopy import *
from cotc import COTC

subprocess.run(["su", "root"])

try:
    ui = xp.ui()
    level = int(ui.stringValue("level"))
    mode = int(ui.stringValue("mode"))
    region = "tw" if level == 2 or level == 4 else "cn"
    c = COTC(region)
except Exception as e:
    xp.toast(e)


def run_script():
    time.sleep(0.5)
    if mode == 1:
        print("点击左侧花田采集点……")
        xp.tap(760, 350)
    elif mode == 2:
        print("点击右侧花田采集点……")
        xp.tap(930, 350)
    else:
        print("不支持的模式，退出脚本……")
        return

    time.sleep(1)
    print("检查获取物品……")
    if not c.wait_item_obtain():
        print("点击对象有误，脚本结束。")
        return

    time.sleep(0.2)
    success = False

    if level == 11 and region == 'cn':
        if xp.matchColor("#F0ECE7", 719, 341, 1.0):
            print("--获取3/6秘技果实--")
            success = True
    elif level == 12 and region == 'cn':
        if xp.matchColor("#F0ECE7", 710, 334, 1.0):
            print("--获取9秘技果实--")
            success = True
    elif level == 13 and region == 'cn':
        if xp.findColor("#FFFFFF", "1|1|#FFFFFF", 550, 339, 552, 341):
            print("--获取12/15/18/21/24/27秘技果实--")
            success = True
    elif level == 2 and region == 'tw':
        # if xp.matchColor("#F0ECE7", 725, 345, 1.0):
        if xp.findColor("#FFFFFF", "1|1|#FFFFFF", 550, 339, 552, 341):
            print("--获取12/15/18/21/24/27秘技果实--")
            success = True
    elif level == 4 and region == 'tw':
        if xp.matchColor("#F0ECE7", 704, 337, 1.0):
            print("--获取30秘技果实--")
            success = True

    if success:
        return
    else:
        print("--获取失败--")
        c.restart_cotc()
    run_script()


run_script()
